package com.qigao.mall.web.controller.app.merchant;

import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.WithDrawService;
import com.qigao.mall.commons.dto.WithDrawAuditDto;
import com.qigao.mall.commons.dto.WithDrawOrderResultDto;
import com.qigao.mall.commons.dto.WithDrawQueryDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.exceptions.BusinessException;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.utils.ValidatorUtil;
import com.qigao.mall.web.vo.PageVo;
import com.qigao.mall.web.vo.app.WithDrawAuditVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/app/merchant/withDraw")
@Api(tags = "App商户提现相关API",value = "App商户提现相关API")
public class MerchantWithDrawController {
    private static Logger logger= LoggerFactory.getLogger(MerchantWithDrawController.class);
    @Resource
    WithDrawService withDrawService;

    @ApiOperation("提现审核")
    @PostMapping("/audit")
    public Result withDrawAudit(@RequestBody WithDrawAuditVo withDrawAuditVo, HttpServletRequest request){
        ValidatorUtil.validateEntity(withDrawAuditVo);
        try{
            WithDrawAuditDto withDrawAuditDto=new WithDrawAuditDto();
            BeanUtils.copyProperties(withDrawAuditVo,withDrawAuditDto);
            withDrawAuditDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            return withDrawService.withDrawAudit(withDrawAuditDto);
        }catch (BusinessException be){
            logger.error("提现审核异常:{}",be);
            return Result.newError(be.getCode(),be.getMsg());
        }catch (Exception e){
            logger.error("提现审核发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    //2.查询提现单列表
    @GetMapping("/findWithDrawPage")
    public Result<PageInfo<WithDrawOrderResultDto>> findWithDrawPage(@RequestBody PageVo pageVo, HttpServletRequest request){
        try{
            WithDrawQueryDto withDrawQueryDto=new WithDrawQueryDto();
            BeanUtils.copyProperties(pageVo,withDrawQueryDto);
            return withDrawService.findWithDrawPage(withDrawQueryDto);
        }catch (Exception e){
            logger.error("提现单列表查询发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }


}
